/**
 * 
 */
package com.mywind.timmer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;

import com.mywind.entity.windfarm.DiagnoseEntity;
import com.mywind.entity.windfarm.WindFarmEntity;
import com.mywind.service.windfarm.IDiagnoseServ;

/**
 * @author a01513
 *
 */
public class ETLExtractor extends TimerTask {

	@Autowired
	private IDiagnoseServ service;
	
	@Override
	public void run() {
		
		List<WindFarmEntity> list = service.getAllWindFarm();
		for(WindFarmEntity wfe:list){
			if(wfe.getEtl() == 1){
				String farmid = wfe.getFarmid();
				Thread etlThread = new Thread(new ExtractorThread(farmid));
				etlThread.start();
			}
		}
	}
	
	private class ExtractorThread implements Runnable{
		
		private String farmid;
		
		public ExtractorThread(String farmid){
			this.farmid = farmid;
		}

		@Override
		public void run() {
			service.setETLStatus(this.farmid, 2);
			//抽数代码
			String basePath = "/diagnose/" + this.farmid + "/";
			File farmDir = new File(basePath);
			List<DiagnoseEntity> list = new ArrayList<DiagnoseEntity>();
			String lastDate = "";
			for(File turbineDir:farmDir.listFiles()){
				if(turbineDir.isDirectory()){
					int wcode = Integer.parseInt(turbineDir.getName());
					File statusCodeDir = new File(basePath + turbineDir.getName() + "/Statuscode/");
					System.out.println("statusCodeDir:" + basePath + turbineDir.getName() + "/Statuscode/");
					File[] txtFiles = statusCodeDir.listFiles();
					for(File txt:txtFiles){
						System.out.println("current file name is:" + txt.getName());
						if(txt.getName().endsWith(".txt")){
							System.out.println("Extracting " + txt.getName() + " file...");
							try{
								BufferedReader br = new   BufferedReader(new InputStreamReader(new FileInputStream(txt), "utf-8"));
								String sLine = null;   
							    while((sLine = br.readLine()) != null){
								    if(sLine.trim().length() > 0)
									   if(sLine.charAt(0) < '0' || sLine.charAt(0) > '9'){
										   continue;
									   }
								    if("".equals(sLine.trim()))
									   continue;
								    StringBuilder strBuilder = new StringBuilder(sLine);
								    String[] result = strBuilder.toString().split("\\s+");
								    DiagnoseEntity de = new DiagnoseEntity();
								    de.setFcode(this.farmid);
								    de.setWcode(wcode);
								    de.setTrigkey(result[2]);
								    de.setDesc(result[3]);
								    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
								    de.setTimes(sdf.parse(result[0]+" "+result[1].split(",")[0]));
								    de.setMsecond(Integer.parseInt(result[1].split(",")[1]));
								    if(result.length == 9){
								    	if(!"".equals(result[5]))
								    		de.setGpower(Double.parseDouble(result[5]));
								    	if(!"".equals(result[6]))
								    		de.setWspeed(Double.parseDouble(result[6]));
								    	if(!"".equals(result[7]))
								    		de.setGspeed(Double.parseDouble(result[7]));
								    	if(!"".equals(result[8]))
								    		de.setPangle(Double.parseDouble(result[8]));
								    }else if(result.length == 10){
								    	if(!"".equals(result[6]))
								    		de.setGpower(Double.parseDouble(result[6]));
								    	if(!"".equals(result[7]))
								    		de.setWspeed(Double.parseDouble(result[7]));
								    	if(!"".equals(result[8]))
								    		de.setGspeed(Double.parseDouble(result[8]));
								    	if(!"".equals(result[9]))
								    		de.setPangle(Double.parseDouble(result[9]));
								    }
								    list.add(de);
								    lastDate = txt.getName().substring(17, 24);
							   }
							   br.close();
							}catch(IOException|ParseException e) {
								System.out.println("Invalidate Row, Skip it now...");
								continue;
							}
							if(service.addAllDRecord(list))
								list.clear();
							else
								service.setETLStatus(this.farmid, 1);
						}
					}
				}
			}
			if(!"".equals(lastDate))
				service.updateMtime(farmid, lastDate);
		    service.setETLStatus(this.farmid, 0);
		}
		
	}

}
